package net.wzw.controller;

import net.wzw.constants.HomeConstant;
import net.wzw.entity.HomeFeedRequestEntity;
import net.wzw.entity.RecipesRequestEntity;
import net.wzw.service.HomeFeedService;
import net.wzw.vo.RecipesListResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import net.wzw.enums.BizCodes;
import net.wzw.utils.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author wzw
 * @date 2025/4/14
 * @Description 首页feed流控制器  1.获取首页Feed流食谱信息 2.生成本地食谱缓存信息列表
 */
@Api("首页feed流")
@RestController
@RequestMapping("/api/home/v1")
@Slf4j
public class HomeController {

    @Autowired
    private HomeFeedService homeFeedService;

    /*
     * Description:  生成feed流信息
     * @param null
     * @Return: {@link null}
     * @Author: Administrator
     * @Date: 2025/4/16 9:57
     */
    public ApiResult feed(@ApiParam("feed信息请求实体") @RequestBody HomeFeedRequestEntity homeFeedRequestEntity){

         log.info("首页feed流开始，homeFeedRequest:{}",homeFeedRequestEntity);

         //检测参数信息
        if(Objects.isNull(homeFeedRequestEntity) || Objects.isNull(homeFeedRequestEntity.getPage())){

            return  ApiResult.doResult(BizCodes.COMMON_PARAM_ERROR);

        }
        //获取用户信息 TODO 后续使用分布式缓存替代
        //LoginUser loginUser = LoginInterceptor.threadLocal.get();

        //封装请求参数
        RecipesRequestEntity recipesRequestEntity = new RecipesRequestEntity();

        recipesRequestEntity.setUserId(homeFeedRequestEntity.getUserId());

        recipesRequestEntity.setFeedVersion(homeFeedRequestEntity.getFeedVersion());

        recipesRequestEntity.setPullRefresh(homeFeedRequestEntity.getPullRefresh());

        recipesRequestEntity.setPage(homeFeedRequestEntity.getPage());

        recipesRequestEntity.setPageSize(HomeConstant.DEFAULT_PAGE_SIZE);

        try {

            RecipesListResultVO list = homeFeedService.getHomeFeedRecipesList(recipesRequestEntity);

            return ApiResult.doSuccess(list);

        }catch (Exception e){

            log.error("首页feed流获取出现异常,homeFeedRequest:{},error:{}",homeFeedRequestEntity,e);
        }

        return  ApiResult.doResult(BizCodes.COMMON_SERVER_ERROR);

    }

    @ApiOperation("生成首页feed流内容")
    @GetMapping("/gen_feed")
    public ApiResult generatorFeed(){

        return  ApiResult.doSuccess(homeFeedService.generatorFeed());

    }




}
